Use Keras to transfer image style with ibm-watson-machine-learning

This notebook contains the steps and code required to demonstrate style transfer technique using Watson Machine Learning Service. It introduces commands for getting data, training_definition persistance to Watson Machine Learning repository and model training.

Some familiarity with Python is helpful. This notebook uses Python 3.8.

Learning goals

The learning goals of this notebook are:

Contents

  1. Setup
  2. Create model definition
  3. Create experiment
  4. Train model
  5. Results
  6. Clean up
  7. Summary and next steps

1. Set up the environment

Before you use the sample code in this notebook, you must perform the following setup tasks:

Connection to WML

Authenticate the Watson Machine Learning service on IBM Cloud. You need to provide platform api_key and instance location.

You can use IBM Cloud CLI to retrieve platform API Key and instance location.

API Key can be generated in the following way:

ibmcloud login
ibmcloud iam api-key-create API_KEY_NAME

In result, get the value of api_key from the output.

Location of your WML instance can be retrieved in the following way:

ibmcloud login --apikey API_KEY -a https://cloud.ibm.com
ibmcloud resource service-instance WML_INSTANCE_NAME

In result, get the value of location from the output.

Tip: Your Cloud API key can be generated by going to the Users section of the Cloud console. From that page, click your name, scroll down to the API Keys section, and click Create an IBM Cloud API key. Give your key a name and click Create, then copy the created key and paste it below. You can also get a service specific url by going to the Endpoint URLs section of the Watson Machine Learning docs. You can check your instance location in your Watson Machine Learning (WML) Service instance details.

You can also get service specific apikey by going to the Service IDs section of the Cloud Console. From that page, click Create, then copy the created key and paste it below.

Action: Enter your api_key and location in the following cell.

Install and import the ibm-watson-machine-learning package

Note: ibm-watson-machine-learning documentation can be found here.

Working with spaces

First of all, you need to create a space that will be used for your work. If you do not have space already created, you can use Deployment Spaces Dashboard to create one.

Tip: You can also use SDK to prepare the space for your work. More information can be found here.

Action: Assign space ID below

You can use list method to print all existing spaces.

To be able to interact with all resources available in Watson Machine Learning, you need to set space which you will be using.

1.1 Working with Cloud Object Storage

Import the Boto library, which allows Python developers to manage COS.

Action: Enter your COS credentials in the following cell.
You can find these credentials in your COS instance dashboard under the Service credentials tab.
Note the HMAC key, described in set up the environment is included in these credentials.

Create the Boto resource by providing type, endpoint_url and credentials.

Create the buckets you will use to store training data and training results.

Note: Bucket names must be unique.

You have now created two new buckets.
Display a list of buckets for your COS instance to verify that the buckets were created.

1.2 Download training data and upload it to COS buckets

Download your training data and upload them to the 'training-data' bucket. Then, create a list of links for training dataset.

The following code snippet creates the STYLE_DATA folder and downloads the files from the links to the folder.

Base image: Cracow - main market square

Style image 1: Vincent Van Gogh - Starry Night


Style image 2: Kandinsky Lyrisches

Upload the data files to the created buckets.

Let's see the list of all the buckets and their contents.

1.3 Create connections to a COS bucket.

You are done with COS, and you are now ready to train your model!

2. Create model definitions

2.1 Prepare model definition metadata

Hint: The final effect depends on number of iterations, and that the number of iterations impacts the training time.

2.2 Get sample model definition content file from git

Hint: You may need to install wget using following command !pip install wget

Tip: Convert below cell to code and run it to see model deinition's code.

!unzip -oqd . STYLE.zip && cat style_transfer.py

2.3 Publish model definitions

Store definition 1

Store definition 2

List the stored definitions

3. Create deep learning experiment

3.1 Prepare experiment metadata

3.2 Publish experiment to repository

3.3 Get experiment details

4. Train model

4.1 Prepare models definitions metadata

4.2 Train models in background

4.3 Get training id and status

4.4 Get trainings details

List trainings

Cancel training

You can cancel the training run by calling the method below.
Tip: If you want to delete train runs and results add hard_delete=True as a parameter.
Tip: Pick one of training_id_1 or training_id_2.

client.training.cancel(training_id_1)

5. Results - transferred styles images

Download transfered transfered photos of Cracow from COS

Cracow

Transfered Images


6. Clean up

If you want to clean up all created assets:

please follow up this sample notebook.

7. Summary and next steps

You successfully completed this notebook! You learned how to use ibm-watson-machine-learning-client to run experiments.

Citations

Author

Jan Sołtysik, Intern in Watson Machine Learning.

Copyright © 2020, 2021 IBM. This notebook and its source code are released under the terms of the MIT License.